package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.Student;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.IStudentService;
import com.hzit.util.PageResult;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student> implements IStudentService {

	//1. 查询所有学生列表
	@Override
	public List<StudentDTO> findAll() {
		return this.list().stream().map(m -> {
			StudentDTO dto = new StudentDTO();
			BeanUtil.copyProperties(m,dto,true);
			return dto;
		}).collect(Collectors.toList());

	}
	//2. 分页查询
	@Override
	public PageResult<StudentDTO> findPage(int page, int pageSize) {
		//2.1 得到page对象
		Page<Student> studentPage = this.page(new Page<>(page, pageSize));
		//2.2 得到每页的记录结果
		List<Student> records = studentPage.getRecords();
		//2.3 处理分页的结果集
		List<StudentDTO> collect = records.stream().map(m -> {
			StudentDTO dto = new StudentDTO();
			BeanUtil.copyProperties(m, dto, true);
			return dto;
		}).collect(Collectors.toList());
		//2.4 返回分页对象
		return new PageResult<>(collect,studentPage.getPages(),page);
	}
	//3. 添加学生
	@Override
	public void insert(StudentDTO dto) {
		Student student = new Student();
		BeanUtil.copyProperties(dto,student,true);
		this.save(student);
	}

	//4. 修改学生
	@Override
	public void update(StudentDTO dto) {
		Student student = new Student();
		BeanUtil.copyProperties(dto,student,true);
		this.updateById(student);
	}
}
